// pages/card/index/index.js
const app = getApp()
import http from '../../../utils/request.js';
import alert from '../../../utils/alert.js';
import { $wuxDialog } from '../../../wux/index';
import QR from '../../../utils/qrcode.js';
import barcode from '../../../utils/barcode.js';
var UiUtils = require("../../../utils/ui.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myCards: [],
    cardBase: {
      is_card_pay:"0",
      is_self_open_car:"0"
    },
    current:0,
    indicatorDots: false,
    circular: false,
    interval: 2000,
    card: {},
    buttons: [{
      type: 'assertive',
      block: true,
      text: '到前台绑定会员卡',
    },{
        type: 'assertive',
        block: true,
        text: '自助开卡',
      }],
    resultIcon: {
      type: 'info',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCardLists()
    this.getCardBase()
    this.setData({
      current: 0
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getCardLists: function () {
    http.get(app.config.service.getMyCard, { app_id: app.globalData.app_id }).then(data => {
      if (data.length > 0) {
        this.setData({
          myCards: data,
          card: data[0]
        })
      }
    }, err => {
      alert.alert(err)
    });
  },
  getCardBase: function () {
    http.post(app.config.service.getPayConfig, { app_id: app.globalData.app_id }).then(data => {
      this.setData({
        cardBase: data
      })
    }, err => {
      alert(err)
    });
  },
  swiperChange: function (e) {
    var myCards = this.data.myCards;
    this.setData({
      card: myCards[e.detail.currentItemId]
    })
  },
  setPwd:function(e){
    var that=this;
    $wuxDialog().prompt({
      resetOnClose: true,
      title: '提示',
      content: '密码为4位数字',
      fieldtype: 'number',
      password: !0,
      defaultText: '',
      placeholder: '请输入新的密码',
      maxlength: 4,
      onConfirm(e, response) {
        console.log(response)
        http.post(app.config.service.setCardPwd, { 
          app_id: app.globalData.app_id,
          card_no:that.data.card.card_no,
          password: response
          }).then(data => {
            alert.toast('密码更新成功');
        }, err => {
          alert.alert(err)
        });
      },
    })
  },
  bindCard:function(){
    // wx.navigateTo({
    //   url: '../bind/index',
    // })
    alert.alert('到吧台添加会员卡')
  },
  showCard: function (e) {
    var card_no = e.currentTarget.dataset.card.card_no
    var w = UiUtils.rpx2px(680)
    var h = UiUtils.rpx2px(200)
    barcode.createBarcode('barcodeCanvas', card_no, w, h);
    var w2 = UiUtils.rpx2px(550)
    QR.qrApi.draw(card_no, "qrcodeCanvas", w2, w2);
    var code = barcode.formatBarCode(card_no, " ")
    var barNumber = card_no
    this.setData({
      barVisible: true,
      barNumber: card_no,
      barcode: code
    })
  },
  onBarVisibleClose: function () {
    this.setData({
      barVisible: false,
    })
  }
})